#include "criterion.h"

criterion* createCriterion(criterionOperator op, criterionField field, char* data)
{
  criterion* c = malloc(sizeof(criterion));
  c->operator = op;
  c->field = field;
  c->data = strdup(data);
  return c;
}

void freeCriterion(criterion* c)
{
  free(c->data);
  free(c);
}

int validateCriterion(criterion* c, file* f)
{
  switch(c->field)
    {
    case CRIT_FILENAME:
      {
	if (c->operator == CRIT_EQ)
	  return (strstr(c->data,f->fileName) != NULL);
	else
	  return 0;
      } break;
    case CRIT_FILESIZE:
      {
	switch(c->operator)
	  {
	  case CRIT_EQ:
	    {
	      int integerData = atoi(c->data);
	      if (integerData != 0)
		return integerData == f->length;
	      else
		return 0;
	    } break;
	  case CRIT_L:
	    {
	      int integerData = atoi(c->data);
	      if (integerData != 0)
		return integerData > f->length;
	      else
		return 0;

	    } break;
	  case CRIT_G:
	    {
	      int integerData = atoi(c->data);
	      if (integerData != 0)
		return integerData < f->length;
	      else
		return 0;
	    } break;
	  default:
	    {
	      return 0;
	    }
	  }
      } break;
    default:
      {
	printf("ERROR : unknown criterion\n");
	return -1;
      }
    }
  return 0;
}

void dumpCriterion(criterion* c)
{
  DBG("criterion : %d %d %s\n", c->operator, c->field, c->data);
}
